using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using AntEater.ComponentModel;
 
namespace AntEater.Runtime.UI
{
    /// <summary>
    /// Displays an external build properties collection
    /// </summary>
    internal partial class ExternalBuildPropertiesListView : ListView
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="ExternalBuildPropertiesListView"/> class.
        /// </summary>
        public ExternalBuildPropertiesListView() {
            InitializeComponent();
            this._machineExternalBuildProperties = new ExternalBuildPropertyCollection();
            this._solutionExternalBuildProperties = new ExternalBuildPropertyCollection();
            this._buildFileExternalBuildProperties = new ExternalBuildPropertyCollection();
        }

        private ExternalBuildPropertyCollection _machineExternalBuildProperties;

        /// <summary>
        /// Gets the machine external build properties.
        /// </summary>
        /// <value>The machine external build properties.</value>
        [Browsable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public ExternalBuildPropertyCollection MachineExternalBuildProperties {
            get { return _machineExternalBuildProperties; }
        }

        private ExternalBuildPropertyCollection _solutionExternalBuildProperties;

        /// <summary>
        /// Gets the solution external build properties.
        /// </summary>
        /// <value>The solution external build properties.</value>
        [Browsable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public ExternalBuildPropertyCollection SolutionExternalBuildProperties {
            get { return _solutionExternalBuildProperties; }
        }

        private ExternalBuildPropertyCollection _buildFileExternalBuildProperties;

        /// <summary>
        /// Gets the build file external build properties.
        /// </summary>
        /// <value>The build file external build properties.</value>
        [Browsable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public ExternalBuildPropertyCollection BuildFileExternalBuildProperties {
            get { return _buildFileExternalBuildProperties; }
        }

        private ExternalBuildProperty _selectedExternalBuildProperty;

        /// <summary>
        /// Gets the selected external build property.
        /// </summary>
        /// <value>The selected external build property.</value>
        [Browsable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public ExternalBuildProperty SelectedExternalBuildProperty {
            get { return _selectedExternalBuildProperty; }
        }

        /// <summary>
        /// Forces the control to invalidate its client area and immediately redraw itself and any child controls.
        /// </summary>
        /// <PermissionSet><IPermission class="System.Security.Permissions.EnvironmentPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true"/><IPermission class="System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true"/><IPermission class="System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Flags="UnmanagedCode, ControlEvidence"/><IPermission class="System.Diagnostics.PerformanceCounterPermission, System, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true"/></PermissionSet>
        public override void Refresh() {

            // Store selected property
            ExternalBuildProperty selectedProperty = null;
            if (this.SelectedItems.Count > 0) {
                selectedProperty = (ExternalBuildProperty)this.SelectedItems[0].Tag;
            }

            // Clear listview
            this.Items.Clear();

            if (_machineExternalBuildProperties.Count > 0) {
                //Add group
                ListViewGroup group = this.Groups.Add("MACHINE", "External Build Properties (Machine)");
                this.RefreshItems(_machineExternalBuildProperties, selectedProperty, group);
            }

            if (_solutionExternalBuildProperties.Count > 0) {
                // Add group
                ListViewGroup group = this.Groups.Add("SOLUTION", "External Build Properties (Solution)");
                this.RefreshItems(_solutionExternalBuildProperties, selectedProperty, group);
            }

            if (_buildFileExternalBuildProperties.Count > 0) {
                // Add group
                ListViewGroup group = this.Groups.Add("BUILDFILE", "External Build Properties (Build File)");
                this.RefreshItems(_buildFileExternalBuildProperties, selectedProperty, group);
            }

            base.Refresh();
        }

        /// <summary>
        /// Refreshes the items.
        /// </summary>
        /// <param name="items">The items.</param>
        /// <param name="selectedItem">The selected item.</param>
        /// <param name="group">The group.</param>
        private void RefreshItems(ExternalBuildPropertyCollection items, ExternalBuildProperty selectedItem, ListViewGroup group) {
            
            // Sort all external build properties
            SortedList<string, ExternalBuildProperty> properties = new SortedList<string, ExternalBuildProperty>(StringComparer.InvariantCultureIgnoreCase);
            foreach (ExternalBuildProperty property in items) {
                properties.Add(property.Name, property);
            }

            // Add all external properties
            foreach (ExternalBuildProperty property in properties.Values) {
                ListViewItem item = this.Items.Add(property.Name, "ExternalBuildProperty");
                item.SubItems.Add(property.Value);
                item.Group = group;
                item.Selected = (property == selectedItem);
                item.Tag = property;

            }
        }

        /// <summary>
        /// Raises the <see cref="E:System.Windows.Forms.ListView.SelectedIndexChanged"></see> event.
        /// </summary>
        /// <param name="e">An <see cref="T:System.EventArgs"></see> that contains the event data.</param>
        protected override void OnSelectedIndexChanged(EventArgs e) {

            if (this.SelectedItems.Count >= 1) {
                _selectedExternalBuildProperty = (ExternalBuildProperty)this.SelectedItems[0].Tag;
            } else {
                _selectedExternalBuildProperty = null;
            }

            base.OnSelectedIndexChanged(e);
        }
    }
}
